package com.pickgliss.bitmapEngin.sound
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	
	public class AnimationSound
	{
		private var _id:String;
		private var _managedCount:int;
		private var _sound:Sound;
		public function AnimationSound(id:String = "",managedCount:int = 0)
		{
			_channels = new Dictionary();
			_id = id;
			_managedCount = managedCount;
			var soundClass:Class = getDefinitionByName(id) as Class;
			_sound = new soundClass();
		}
		
		public function get id():String
		{
			return _id;
		}
		
		public function set id(value:String):void
		{
			_id = value;
		}
		
		public function get managedCount():int
		{
			return _managedCount;
		}
		
		public function set managedCount(value:int):void
		{
			_managedCount = value;
		}
		private var _channels:Dictionary;
		
		public function play(clipid:String):void
		{
			if(_playingCount < _managedCount)
			{
				_channels[clipid] = _sound.play();
				_playingCount ++;
			}
		}
		
		public function stop(clipid:String):void
		{
			var channel:SoundChannel = _channels[clipid];
			if(channel)
			{
				channel.stop();
				_playingCount --;
			}
		}
		
		public function stopAll():void
		{
			for each (var element:SoundChannel in _channels)
			{
				element.stop();
			}
		}
		
		private var _playingCount:int;
		public function get playingCount():int
		{
			return _playingCount;
		}
	}
}